<?php

defined('MYLITS') ? '' : exit('No direct script access allowed');

/**
 * @version     1.0.0
 * @package     mylits engine
 * @license     GNU General Public License version 3
 * @author      Arfan Fudyartanto D N <arfan@mylits.com> - http://engine.mylits.com
 */
class mainModel extends Mylits_Model {

    public function __construct() {
        parent::__construct();
        $this->setConnectionNumber(0);
        $this->setSqlFile('main');
    }

    /**
     * Method to get widget list
     * 
     * @param string $position -> position name of widget
     * @return array -> widget list
     */
    public function getWidgetList($position) {
        $query = $this->openQuery('get_widgets_list', array($position));
        $user = Mylits_User::getInstance();
        $filter = "";
        if (!$user::$guest) {
            $group = $user::getGroupList();
            $filter = ' OR b.`core_widgets_usermap_usergroup_id` IN (' . implode(',', $group) . ')';
        }
        $query = str_replace('--filter--', $filter, $query);
        $result = $this->runQuery($query);
        return $result;
    }

    /**
     * Method to check component permission
     * 
     * @param string $component -> component name
     * @return boolean
     */
    public function checkComponentPermission($component) {
        $query = $this->openQuery('check_component_permission', array($component));
        $result = $this->runQuery($query);
        if (!empty($result)) {
            if ($result[0]['count'] >= 1) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Method to get group list of certain user
     * 
     * @param integer $userId
     * @return array -> group list
     */
    public function getUserGroupList($userId) {
        $query = $this->openQuery('get_usergroup_list', array($userId));
        $tmp_result = $this->runQuery($query);
        $result = array();
        foreach ($tmp_result as $value) {
            $result[] = $value['group'];
        }
        return $result;
    }
    
    public function getRealPath($pathAlias){
        $query = $this->openQuery('get_real_path', array($pathAlias));
        $result = $this->runQuery($query);
        if(!empty($result)){
            return $result[0]['path'];
        }else{
            return $pathAlias;
        }
    }

    public function getActiveTemplates(){
        $query = $this->openQuery('get_active_template');
        $result = $this->runQuery($query);
        if(!empty($query)){
            return $result[0]['core_templates_name'];
        }else{
            return 'default';
        }
    }
}

?>
